package com.iceclean.daoImpl;

import com.iceclean.dao.PostBarDao;
import com.iceclean.po.Post;
import com.iceclean.po.PostBar;
import com.iceclean.util.JdbcUtil;

import java.util.List;

/**
 * @author : Ice'Clean
 * @date : 2021-05-04
 *
 * 贴吧表操作实体类
 */
public class PostBarDaoImpl implements PostBarDao {
    @Override
    public PostBar findPostBarById(int postBarId) {
        List<PostBar> postBars = JdbcUtil.query("select * from t_post_bar where post_bar_id = " + postBarId, PostBar.class);
        return postBars.size() == 0 ? null : postBars.get(0);
    }

    @Override
    public PostBar findPostBarByName(String postBarName) {
        List<PostBar> postBars = JdbcUtil.query("select * from t_post_bar where post_bar_name = ?", PostBar.class, postBarName);
        return postBars.size() == 0 ? null : postBars.get(0);
    }

    @Override
    public List<PostBar> findPostBarsByPageForChecked(int page, int num) {
        List<PostBar> postBars = JdbcUtil.query("select * from t_post_bar where post_bar_check = 1 limit " + (page-1)*num + "," + num, PostBar.class);
        return postBars.size() == 0 ? null : postBars;
    }

    @Override
    public List<PostBar> findPostBarsByPageForUnChecked(int page, int num) {
        List<PostBar> postBars = JdbcUtil.query("select * from t_post_bar where post_bar_check = 0 limit " + (page-1)*num + "," + num, PostBar.class);
        return postBars.size() == 0 ? null : postBars;
    }

    @Override
    public List<PostBar> findUserPostBarByPage(int userId, int page, int num) {
        List<PostBar> postBars = JdbcUtil.query("select * from t_post_bar where post_bar_check = 1 and user_id = ? limit " + (page-1)*num + "," + num, PostBar.class, userId);
        return postBars.size() == 0 ? null : postBars;
    }

    /**
     * 搜索查询时，只查询已通过审核的贴吧
     * 对多个字段进行模糊搜索，分别是：贴吧名、贴吧简介
     * @param search 搜索字段
     * @param page 页数
     * @param num 每页的数目
     * @return 符合条件的贴吧集合
     */
    @Override
    public List<PostBar> searchPostBarByPage(String search, int page, int num) {
        List<PostBar> postBarList = JdbcUtil.query("select * from t_post_bar where post_bar_check = 1 and concat(post_bar_name, post_bar_resume) like ? limit " + (page-1)*num + "," + num, PostBar.class, "%"+search+"%");
        return postBarList.size() == 0 ? null : postBarList;
    }

    @Override
    public int getAllSearchForNumber(String search) {
        return JdbcUtil.query("select post_bar_id from t_post_bar where post_bar_check = 1 and concat(post_bar_name, post_bar_resume) like ? ", PostBar.class, "%"+search+"%").size();
    }

    @Override
    public int getNumberOfAllPostBarForChecked() {
        return JdbcUtil.query("select post_bar_id from t_post_bar where post_bar_check = 1", PostBar.class).size();
    }

    @Override
    public int getNumberOfAllPostBarForUnChecked() {
        return JdbcUtil.query("select post_bar_id from t_post_bar where post_bar_check = 0", PostBar.class).size();
    }


    @Override
    public int createPostBar(int userId, String postBarName, String postBarResume) {
        return JdbcUtil.insert("t_post_bar", new PostBar(userId, postBarName, postBarResume));
    }

    @Override
    public boolean updatePostBar(PostBar postBar) {
        return JdbcUtil.update("t_post_bar", "post_bar_id = " + postBar.getPostBarId(), postBar);
    }

    @Override
    public boolean deletePostBar(int postBarId) {
        return JdbcUtil.delete("t_post_bar", "post_bar_id = " + postBarId);
    }
}
